package controllers

import (
	"crypto/md5"
	"encoding/hex"
	"github.com/astaxie/beego"
	"home/models"
	"home/service"
	"strconv"
	"time"
)

type VideoDetailsController struct {
	beego.Controller
}

func md5V(str string) string {
	h := md5.New()
	h.Write([]byte(str))
	return hex.EncodeToString(h.Sum(nil))
}


func (this *VideoDetailsController) Get() {
	// 接受视频编号
	video_number := this.Ctx.Input.Param(":video_number")
	this.TplName = "details.tpl"
	// 获取菜单数据
	var datalist []models.HomeMenu
	datalist, err := models.GetMenu()
	if err != nil {
		beego.Error(err)
	}
	this.Data["Menu"] = datalist
	// 获取趋势数据
	var dataList []models.HomeTrending
	dataList,err = models.GetTrendingList()
	if err != nil {
		beego.Error(err)
	}
	this.Data["TrendingList"] = dataList

	var details models.Video
	details, err = models.GetVideDetails(video_number)
	if err != nil {
		beego.Error(err)
	}
	this.Data["Details"] = details
	// 生成md5格式：key:时间戳:视频编号
	token := md5V("shiguang:" +strconv.FormatInt(time.Now().Unix(),10)+":"+details.VideoNumber)
	redis := &service.Redis_tool{}
	redis.Init(15)
	redisKey := "video_token:" + token
	redis.Set(redisKey, details.VideoSrc)
	redis.Expire(redisKey, 600)

	this.Data["Token"] = token
}
